package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAttachment;
import cc.comac.mom.dao.entity.QualityAttachmentDao;
import cc.comac.mom.dao.entity.QualityAttachmentGroupDao;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.entity.QualityAttachmentGroupEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualityAttachmentDataHandler extends ServiceImpl<QualityAttachmentDao, QualityAttachmentEntity>{

	@Autowired
	private QualityAttachmentDao qualityAttachmentDao;
	
	@Autowired
	private QualityAttachmentGroupDao qualityAttachmentGroupDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean insertBatch(List<QualityAttachment> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		ArrayList<QualityAttachmentEntity> listEntity = new ArrayList<QualityAttachmentEntity>();
	//	ArrayList<Map<String,Object>> listFileInfo = new ArrayList<Map<String, Object>>();
		for(QualityAttachment item :list) {
			if(item.getInnerData()==null) {
				throw new RuntimeException("数据为空");	
			}
			baseEntityDataUtil.setBaseEntityData(item.getInnerData());
//			HashMap<String,Object> map = new HashMap<String, Object>();
//			map.put("id", item.getInnerData().getId());
//			map.put("fileName", item.getInnerData().getRealName());
			//listFileInfo.add(map);
			listEntity.add(item.getInnerData());
			

		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			throw new RuntimeException("保存失败");
		}
		resultBean.setObject(listEntity);
		return resultBean;
		 
	}
	
	public ResultBean deleteByAttachmentId(long attachmentId) {
		ResultBean resultBean = new ResultBean(true);
		QualityAttachmentEntity attachmentEntity = qualityAttachmentDao.selectById(attachmentId);
		if(attachmentEntity == null) {
			throw new RuntimeException("文件信息不存在");	
		}
		baseEntityDataUtil.deleteBaseEntityData(attachmentEntity);
		int updateById = qualityAttachmentDao.updateById(attachmentEntity);
		if(updateById<1) {
			throw new RuntimeException("删除文件失败");
		}
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("attachment_id",attachmentId);
		List<QualityAttachmentGroupEntity> selectByMap = qualityAttachmentGroupDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {
			QualityAttachmentGroupEntity qualityAttachmentGroupEntity = selectByMap.get(0);
			baseEntityDataUtil.deleteBaseEntityData(qualityAttachmentGroupEntity);
			int updateById2 = qualityAttachmentGroupDao.updateById(qualityAttachmentGroupEntity);
			if(updateById2<1) {
				throw new RuntimeException("删除文件组信息失败");
			}
		}
		return resultBean;
	}
	
	public List<QualityAttachment> getByAttachmentIds(List<Long> attachmentIds) {

		if(attachmentIds == null || attachmentIds.size()<1) {
			return null;	
		}
		List<QualityAttachmentEntity> selectBatchIds = qualityAttachmentDao.selectBatchIds(attachmentIds);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			return null;	
		}
		ArrayList<QualityAttachment> listQualityAttachment = new ArrayList<QualityAttachment>();
		for(QualityAttachmentEntity item :selectBatchIds) {
			QualityAttachment qualityAttachment = new QualityAttachment();
			qualityAttachment.setInnerData(item);
			listQualityAttachment.add(qualityAttachment);
		}
		return listQualityAttachment;
		
	}
	
	
}
